package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.example.demo.service.serviceimpl.UserServiceImpl;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@RestController
@ResponseBody
public class UserController {

    @Autowired(required = false)
    UserService userService=new UserServiceImpl();
    String account;

    @PostConstruct
    public void init(){
        this.userService=new UserServiceImpl();
    }

    @RequestMapping("/login")
    public String login(String account,String password) {
        User user=userService.selectUserByMessage(account, password);
        JSONObject jsonObject=new JSONObject();
        if (user!=null){
            jsonObject.put("code",0);
            jsonObject.put("msg","登陆成功！");
            this.account=user.getAccount();
        }
        else{
            jsonObject.put("code",0);
            jsonObject.put("msg","登陆失败！");
        }
        return jsonObject.toString();
    }

    @RequestMapping("/register")
    public String register(String account,String password){
        JSONObject jsonObject=new JSONObject();
        if(selectUserByAccount(account)==null){
            userService.saveUser(account,password);
            jsonObject.put("code",0);
            jsonObject.put("msg","注册成功！");
        }
        else{
            jsonObject.put("code",0);
            jsonObject.put("msg","注册失败！");
        }
        return jsonObject.toString();
    }

    @RequestMapping("/account_check")
    public String accountCheck(String account, HttpServletResponse response) throws IOException {
        User user=userService.selectUserByAccount(account);
        if(user!=null){
            return "no";
        }
        else {
            return "yes";
        }
    }

    @RequestMapping("/index")
    public String mainPage() {
        return "index";
    }

    @RequestMapping("/user_account")
    public String model(){
        System.out.println("account："+this.account);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg",this.account);
        return jsonObject.toString();
    }

    @RequestMapping("/edit")
    public String editUser(User user){
        User user1=selectUserByAccount(user.getAccount());
        user.setId(user1.getId());
        user.setPassword(user1.getPassword());
        userService.editUser(user);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","保存成功！");
        return jsonObject.toString();
    }

    @RequestMapping("/finduser")
    public String findUser(){
        User user1=selectUserByAccount(this.account);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("account",user1.getAccount());
        jsonObject.put("name",user1.getName());
        jsonObject.put("sex",user1.getSex());
        jsonObject.put("birthday",user1.getBirthday());
        jsonObject.put("cellphone",user1.getCellphone());
        jsonObject.put("email",user1.getEmail());
        jsonObject.put("qq",user1.getQq());
        System.out.println(jsonObject.toString());
        return jsonObject.toString();
    }

    @RequestMapping("/uploadpass")
    public String uploadPass(String password){
        User user=selectUserByAccount(this.account);
        user.setPassword(password);
        System.out.println("修改后密码的信息："+user);
        uploadPassword(user);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","修改成功！");
        return jsonObject.toString();
    }

    public User selectUserByAccount(String account){
        return userService.selectUserByAccount(account);
    }

    public void uploadPassword(User user){ userService.uploadPassword(user); }
}
